package com.pingan.haofang.searchcloud.common.dto;

import com.google.gson.annotations.Expose;
import com.pingan.haofang.searchcloud.common.constants.QueryOptimizeEnum;
import com.pingan.haofang.searchcloud.common.fetch.StreamFetchGatherInfo;

import java.util.List;

/**
 * 抓取数据结果DTO
 *
 * @author SONGBANGYONG396
 * @date 2018/7/2
 * @since 1.0.0
 */
public class FetchDatasDTO<E> {

    /**
     * 抓取的数据
     */
    private List<E> indexDatas;

    /**
     * 抓取的数据的进度
     */
    private FetchDatasProgress fetchDatasProgress;

    /**
     * 需要跳过的导入成功的数量
     */
    private Integer skipedImportSuccessCount;

    /**
     * 需要跳过的抓取数据的数量
     */
    private Integer skipedIndexDatasSize;

    public FetchDatasDTO() {
    }

    private FetchDatasDTO(Builder builder) {
        indexDatas = builder.indexDatas;
        fetchDatasProgress = builder.fetchDatasProgress;
        skipedImportSuccessCount = builder.skipedImportSuccessCount;
        skipedIndexDatasSize = builder.skipedIndexDatasSize;
    }

    public Integer getSkipedImportSuccessCount() {
        return skipedImportSuccessCount;
    }

    public Integer getSkipedIndexDatasSize() {
        return skipedIndexDatasSize;
    }

    public static Builder newBuilder() {
        return new Builder();
    }


    public List<E> getIndexDatas() {
        return indexDatas;
    }

    public FetchDatasProgress getFetchDatasProgress() {
        return fetchDatasProgress;
    }

    public Integer getThreadSeq() {
        return fetchDatasProgress.threadSeq;
    }

    /**
     * 抓取数据的进程
     */
    public static class FetchDatasProgress {
        /**
         * 总数量
         */
        private Long total;

        /**
         * 每页大小
         */
        private Integer pageSize;


        /**
         * 页码
         */
        private Integer pageNo;


        /**
         * 该次查询所用的LastMaxId
         */
        private Long currentLastMaxId;

        /**
         * 下次查询所用的LastMaxId
         */
        private Long nextLastMaxId;

        /**
         * 线程数量
         */
        private Integer threadNum;

        /**
         * 是否查询优化，1是，0否
         */
        private Integer queryOptimize;

        /**
         * 线程序号
         */
        private Integer threadSeq;

        /**
         * 每个线程的循环次数
         */
        private Integer loopCount;

        /**
         * 导入成功数量
         */
        private Integer importSuccessCount;

        /**
         * 抓取信息
         */
        private StreamFetchGatherInfo streamFetchGatherInfo;

        /**
         * 校准后的抓取信息
         */
        private transient StreamFetchGatherInfo correctStreamFetchGatherInfo;

        /**
         * 抓取数据的数量
         */
        private Integer indexDatasSize;

        public Long getTotal() {
            return total;
        }

        public void setTotal(Long total) {
            this.total = total;
        }

        public Integer getPageSize() {
            return pageSize;
        }

        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }

        public Integer getPageNo() {
            return pageNo;
        }

        public void setPageNo(Integer pageNo) {
            this.pageNo = pageNo;
        }

        public Long getCurrentLastMaxId() {
            return currentLastMaxId;
        }

        public void setCurrentLastMaxId(Long currentLastMaxId) {
            this.currentLastMaxId = currentLastMaxId;
        }

        public Long getNextLastMaxId() {
            return nextLastMaxId;
        }

        public void setNextLastMaxId(Long nextLastMaxId) {
            this.nextLastMaxId = nextLastMaxId;
        }

        public Integer getThreadNum() {
            return threadNum;
        }

        public void setThreadNum(Integer threadNum) {
            this.threadNum = threadNum;
        }

        public Integer getQueryOptimize() {
            return queryOptimize;
        }

        public void setQueryOptimize(Integer queryOptimize) {
            this.queryOptimize = queryOptimize;
        }

        public Integer getThreadSeq() {
            return threadSeq;
        }

        public void setThreadSeq(Integer threadSeq) {
            this.threadSeq = threadSeq;
        }

        public Integer getLoopCount() {
            return loopCount;
        }

        public void setLoopCount(Integer loopCount) {
            this.loopCount = loopCount;
        }

        public Integer getImportSuccessCount() {
            return importSuccessCount;
        }

        public void setImportSuccessCount(Integer importSuccessCount) {
            this.importSuccessCount = importSuccessCount;
        }

        public StreamFetchGatherInfo getStreamFetchGatherInfo() {
            return streamFetchGatherInfo;
        }

        public void setStreamFetchGatherInfo(StreamFetchGatherInfo streamFetchGatherInfo) {
            this.streamFetchGatherInfo = streamFetchGatherInfo;
        }

        public StreamFetchGatherInfo getCorrectStreamFetchGatherInfo() {
            return correctStreamFetchGatherInfo;
        }

        public void setCorrectStreamFetchGatherInfo(StreamFetchGatherInfo correctStreamFetchGatherInfo) {
            this.correctStreamFetchGatherInfo = correctStreamFetchGatherInfo;
        }

        public Integer getIndexDatasSize() {
            return indexDatasSize;
        }

        public void setIndexDatasSize(Integer indexDatasSize) {
            this.indexDatasSize = indexDatasSize;
        }

        private FetchDatasProgress(Builder builder) {
            total = builder.total;
            pageSize = builder.pageSize;
            pageNo = builder.pageNo;
            currentLastMaxId = builder.currentLastMaxId;
            nextLastMaxId = builder.nextLastMaxId;
            threadNum = builder.threadNum;
            queryOptimize = builder.queryOptimize;
            threadSeq = builder.threadSeq;
            loopCount = builder.loopCount;
            importSuccessCount = builder.importSuccessCount;
            streamFetchGatherInfo = builder.streamFetchGatherInfo;
            correctStreamFetchGatherInfo = builder.correctStreamFetchGatherInfo;
            indexDatasSize = builder.indexDatasSize;
        }

        public static Builder newBuilder() {
            return new Builder();
        }


        public static final class Builder {
            private Long total;
            private Integer pageSize;
            private Integer pageNo;
            private Long currentLastMaxId;
            private Long nextLastMaxId;
            private Integer threadNum;
            private Integer queryOptimize;
            private Integer threadSeq;
            private Integer loopCount;
            private Integer importSuccessCount;
            private StreamFetchGatherInfo streamFetchGatherInfo;
            private StreamFetchGatherInfo correctStreamFetchGatherInfo;
            private Integer indexDatasSize;

            private Builder() {
            }

            public Builder total(Long val) {
                total = val;
                return this;
            }

            public Builder pageSize(Integer val) {
                pageSize = val;
                return this;
            }

            public Builder pageNo(Integer val) {
                pageNo = val;
                return this;
            }

            public Builder currentLastMaxId(Long val) {
                currentLastMaxId = val;
                return this;
            }

            public Builder nextLastMaxId(Long val) {
                nextLastMaxId = val;
                return this;
            }

            public Builder threadNum(Integer val) {
                threadNum = val;
                return this;
            }

            public Builder queryOptimize(Integer val) {
                queryOptimize = val;
                return this;
            }

            public Builder threadSeq(Integer val) {
                threadSeq = val;
                return this;
            }

            public Builder loopCount(Integer val) {
                loopCount = val;
                return this;
            }

            public Builder importSuccessCount(Integer val) {
                importSuccessCount = val;
                return this;
            }

            public Builder streamFetchGatherInfo(StreamFetchGatherInfo val) {
                streamFetchGatherInfo = val;
                return this;
            }

            public Builder correctStreamFetchGatherInfo(StreamFetchGatherInfo val) {
                correctStreamFetchGatherInfo = val;
                return this;
            }

            public Builder indexDatasSize(Integer val) {
                indexDatasSize = val;
                return this;
            }

            public FetchDatasProgress build() {
                return new FetchDatasProgress(this);
            }
        }
    }

    public static final class Builder<E> {
        private List<E> indexDatas;
        private FetchDatasProgress fetchDatasProgress;
        private Integer skipedImportSuccessCount;
        private Integer skipedIndexDatasSize;

        private Builder() {
        }

        public Builder indexDatas(List<E> val) {
            indexDatas = val;
            return this;
        }

        public Builder fetchDatasProgress(FetchDatasProgress val) {
            fetchDatasProgress = val;
            return this;
        }

        public Builder skipedImportSuccessCount(Integer val) {
            skipedImportSuccessCount = val;
            return this;
        }

        public Builder skipedIndexDatasSize(Integer val) {
            skipedIndexDatasSize = val;
            return this;
        }

        public FetchDatasDTO build() {
            return new FetchDatasDTO(this);
        }
    }

    /**
     * 获取进度信息，线程数量_线程序号_上一次最大ID_总页数_页码_每页数量
     *
     * @return
     */
    public String getProgress(QueryOptimizeEnum queryOptimize) {
        if (QueryOptimizeEnum.ENABLE.equals(queryOptimize)) {
            return fetchDatasProgress.loopCount + "_" + fetchDatasProgress.total + "_" + fetchDatasProgress.pageNo + "_"
                    + fetchDatasProgress.pageSize;
        } else {
            return fetchDatasProgress.loopCount + "_" + fetchDatasProgress.pageNo + "_" + fetchDatasProgress.pageSize;
        }
    }

    @Override
    public String toString() {
        return "FetchDatasDTO{" +
                "fetchDatasProgress=" + fetchDatasProgress +
                ", skipedImportSuccessCount=" + skipedImportSuccessCount +
                ", skipedIndexDatasSize=" + skipedIndexDatasSize +
                '}';
    }
}
